//
//  PopoverPresentationController.swift
//  HMWeibo007
//
//  Created by TTang on 15/7/4.
//  Copyright © 2015年 shanyu. All rights reserved.
//

import UIKit

class PopoverPresentationController: UIPresentationController {

    /// 展现视图的frame
    var presentFrame = CGRectZero
    
    //懒加载遮罩视图
    lazy var dummy: UIView = {
        
        let v = UIView()
        v.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        return v
        
    }()
    
    /**
    containerView 容器视图，放置展现出来的视图的
    presentedView() 被展现出来的视图的
    
    */
    
    ///  实例化转场控制器
    ///
    ///  :param: presentedViewController  被‘展现’的控制器
    ///  :param: presentingViewController 文档上说，发起的控制器，Xcode 6 nil，Xcode 7 野指针
    ///
    ///  :returns: 展现控制器
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
        
            prepareDummyView()
    }
    
    
    func close(){
    
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    
    private func prepareDummyView(){
    
        let tap = UITapGestureRecognizer(target: self, action: "close")
        dummy.addGestureRecognizer(tap)
    
    }
    
    ///  容器视图将要重新布局子视图
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        //添加并设置dummyView
        dummy.frame = containerView!.bounds
        containerView?.insertSubview(dummy, atIndex: 0)
        //设置视图大小
        presentedView()?.frame = presentFrame
    }
    
}
